/*************************************************
Author: byron
Date: 12/07/2012
Description:
Notes:
*************************************************/

#ifndef __CActMgr_h__
#define __CActMgr_h__

#include "commerrno.h"
#include "CActCfg.h"
#include "CMineCrystalConfig.h"
#include "CEnemyAtkCity.h"
#include "MsgSplendidAct.pb.h"
#include "CPyramidConfig.h"
#include "CSystemNoticeCfg.h"
#include "CNewbieTaskConfig.h"
#include "MsgTreasureBox.pb.h"
#include "MsgAct.pb.h"

class MsgReqGMPlayer;
class MsgAnsGMPlayer;
class MsgAnsActOpt;
class CPlayerDetailInfo;

class CActMgr
{
public:
    enum CMD_ACT_OPT_SUB_TAG
    {
        //获取全量活动列表 会随命令立即刷出
        CMD_ACT_OPT_SUB_GET_ACT_LIST = 1,

        //下发全量活动列表 等待下一次命令刷出或者显式立即刷出
        CMD_ACT_OPT_SUB_NOTIFY_ACT_LIST = 2,

        //准备数据并单独下发MsgAnsMapInfo中的状态列表和冥想状态
        CMD_ACT_OPT_SUB_NOTIFY_STATUS_TO_SELF = 3,

        //准备数据并下发MsgAnsMapInfo中的状态列表和冥想状态给地图
        CMD_ACT_OPT_SUB_NOTIFY_STATUS_TO_MAP = 4,

        //下发全量活动列表 等待下一次命令刷出或者显式立即刷出
        CMD_ACT_OPT_SUB_NOTIFY_ACT_LIST_TO_ALL = 5,

        //准备数据并更新奖励找回 玩家参加活动
        CMD_ACT_OPT_SUB_REWARD_BACK_SET_ACT = 6,

        //准备数据并更新登录奖励 下发在线玩家
        CMD_ACT_OPT_SUB_NOTIFY_LOGIN_REWARD = 7,

        //准备数据并更新领红包数据
        CMD_ACT_OPT_SUB_RED_PACKET_UPDATE = 8,
    };

    CActMgr()
    {
        _iLastTime = 0;
        ClearSystemStopTime();
        ClearNotifyAndUpdateTime();
    }

    virtual ~CActMgr()
    {
    }

    static int CountSize()
    {
        return CActCfg::CountSize() + CActStatusCfg::CountSize() +
            CSystemNoticeCfg::CountSize();
    }

    int Init(const char * sBuff)
    {
        if (!sBuff)
        {
            return RET_SYS_NULL_POINTER;
        }

        int iRet = 0;
        int iOffset = 0;

        iRet = _stActCfg.Init(sBuff + iOffset);
        CT_RETURN(iRet);
        iOffset += CActCfg::CountSize();

        iRet = _stActStatusCfg.Init(sBuff + iOffset);
        CT_RETURN(iRet);
        iOffset += CActStatusCfg::CountSize();

        iRet = _stSystemNoticeCfg.Init(sBuff + iOffset);
        CT_RETURN(iRet);
        iOffset += CSystemNoticeCfg::CountSize();

        return 0;
    }

    int ParseTime(const string& strDateTime, int& iDateTime, char chType);

    int ParseSegTime(CActCfgData& stCfgData, const string& strSegStart,
            const string& strSegStop);

    int ParseSegTime(CSystemNoticeCfgData& stCfgData,
            const string& strSegStart, const string& strSegStop);

    int LoadConfig(const char* szCfgPath);

    int FindActCfg(short shResID)
    {
        return _stActCfg.FindConfig(shResID);
    }

    CActCfgData& GetActCfg(int iIndex)
    {
        return _stActCfg.GetConfig(iIndex);
    }

    void ClearSystemStopTime()
    {
        _iSysStopTime = 0;
        _szSysStopTime.Set("");
        _szSysStartTime.Set("");
        _iNextNoticeSysStop = 0;
    }

    void SetSysStopTime(int iVal) {_iSysStopTime = iVal;}
    int GetSysStopTime() const {return _iSysStopTime;}
    int GetNextNoticeSysStop() const {return _iNextNoticeSysStop;}

    void SetSysStopTime(const char* szStr) {_szSysStopTime.Set(szStr);}
    void SetSysStartTime(const char* szStr) {_szSysStartTime.Set(szStr);}

    void ClearNotifyAndUpdateTime()
    {
        _ashNotifyAct.Clear();
        _aiUpdateTime.Clear();
        _aiNotifyTip.Clear();
        _aiSysNotice.Clear();
    }

    void AddNotifyAct(short shActID){_ashNotifyAct.AddOneItem(shActID);}

    int GetNotifyActSize() const {return _ashNotifyAct.Size();}

    short GetNotifyAct(int iIndex)
    {
        if (iIndex < 0 || iIndex >= _ashNotifyAct.Size())
        {
            return 0;
        }
        return _ashNotifyAct[iIndex];
    }

    void AddUpdateTime(int iDayTime)
    {
        for (int i = 0; i < _aiUpdateTime.Size(); ++i)
        {
            if (iDayTime == _aiUpdateTime[i])
            {
                return;
            }
        }
        _aiUpdateTime.AddOneItem(iDayTime);
    }

    int GetUpdateTimeSize() const {return _aiUpdateTime.Size();}

    int GetUpdateTime(int iIndex)
    {
        if (iIndex < 0 || iIndex >= _aiUpdateTime.Size())
        {
            return -1;
        }
        return _aiUpdateTime[iIndex];
    }

    void AddNotifyTip(int iVal){_aiNotifyTip.AddOneItem(iVal);}

    int GetNotifyTipSize() const {return _aiNotifyTip.Size();}

    int GetNotifyTip(int iIndex)
    {
        if (iIndex < 0 || iIndex >= _aiNotifyTip.Size())
        {
            return 0;
        }
        return _aiNotifyTip[iIndex];
    }

    void AddSysNotice(int iID){_aiSysNotice.AddOneItem(iID);}

    int GetSysNoticeSize() const {return _aiSysNotice.Size();}

    int GetSysNotice(int iIndex)
    {
        if (iIndex < 0 || iIndex >= _aiSysNotice.Size())
        {
            return 0;
        }
        return _aiSysNotice[iIndex];
    }

    int AddActStatusCfg(CActStatusCfgData& stCfgData)
    {
        return _stActStatusCfg.AddConfig(stCfgData);
    }

    int FindActStatusCfg(short shActID)
    {
        return _stActStatusCfg.FindConfig(shActID);
    }

    CActStatusCfgData& GetActStatusCfg(int iIndex)
    {
        return _stActStatusCfg.GetConfig(iIndex);
    }

    int FindFightEndCfg(int iLevel)
    {
        return _stFightEndConfig.FindLevelConfig(iLevel);
    }

    CFightEndCfgData& GetFightEndCfg(int iIndex)
    {
        return _stFightEndConfig.GetLevelConfig(iIndex);
    }

    bool IsActOpen(short shActID);

    void SetActClose(short shActID, bool bChangeEndTime = false);

    int GetBeginTime(short shActID);

    int GetEndTime(short shActID);

    int GetStopTime(short shActID);

    void RefreshAct();

    void ActStatChange();

    void DealActTips();

    void DealSysStop();

    void DealSystemNotice();

    int GMDealAct(MsgReqGMPlayer& stReq, MsgAnsGMPlayer& stAns);

    int GMDealSystemNotice(MsgReqGMPlayer& stReq, MsgAnsGMPlayer& stAns);

    bool IsSplendidActValid(int iActID);

    bool IsSplendidActValid(CSplendidAct& stActData, unsigned int uiGID, int iActID);

    //精彩活动：合成魔晶触发
    void TriggerCrystalCombine(CSplendidAct& stActData, unsigned int uiGID, unsigned short usCombinedCrystalId, short shCount);

    //精彩活动：等级提升触发
    void TriggerLevelUp(CSplendidAct& stActData, unsigned int uiGID, short shNowLevel);

    //精彩活动：宠物养成大礼
    void TriggerPetLevelUp(CSplendidAct& stActData, unsigned int uiGID, short shNowPetLevel);

    //精彩活动：招募湖中仙女(声望增加触发)
    void TriggerGetPartner(CSplendidAct& stActData, unsigned int uiGID, int iPartnerID);

    //精彩活动：怪物攻城连杀
    void TriggerEacKillEnemy(CSplendidAct& stActData, unsigned int uiGID);

    //精彩活动: 充值送魔晶活动
    void TriggerBuyGoods(CSplendidAct& stActData, unsigned int uiGID, unsigned int uiGoodsID, int iCount, int iUseQCent, int iTotalPay);

    //精彩活动：获得物品
    void TriggerGetItem(CSplendidAct& stActData, unsigned int uiGID, unsigned char ucItemType, int iItemID, int iItemCount);

    //精彩活动：获得卡片
    void TriggerGetCard(CSplendidAct& stActData, unsigned int uiGID, unsigned char ucCardType, unsigned short usCardConfigID, int iCardCount);

    void TriggerSubMoney(unsigned int uiGID, int iGold, int iSilver);

    //精彩活动秒触发器
    void SplendidActSecTick(int iNowTime);

    //获取精彩活动列表信息
    void GetSplendidActListInfo(CSplendidAct& stActData, unsigned int uiGID, MsgAnsSplendidAct& stAns);
    void GetSplendidActListInfo(CSplendidAct& stActData, unsigned int uiGID, int iActID, CSplendidActConfigData& stConfigData, MsgSplendidActInfo& stActInfo);

    //获取精彩活动详细信息
    int GetSplendidActDetailInfo(CSplendidAct& stActData, unsigned int uiGID, int iActID, CSplendidActConfigData& stConfigData, MsgAnsSplendidAct& stAns);
    int GetSplendidActDetailInfo(CSplendidAct& stActData, unsigned int uiGID, int iActID, MsgAnsSplendidAct& stAns);

    //领取精彩活动奖励
    int GetSplendidActBonus(CSplendidAct& stActData, unsigned int uiGID, int iActID, int iPos, MsgAnsSplendidAct& stAns);

    //下发精彩活动摘要信息
    void SendSplendidActListToFront(CSplendidAct& stActData, unsigned int uiGID);

    //下发精彩活动详细信息
    void SendSplendidActDetailInfoToFront(CSplendidAct& stActData, unsigned int uiGID, int iActID);

    //下发当前已开启的活动
    void NotifyActListToAll();

    void NotifyActListToSelf(unsigned int uiGID);

    void GetActList(CPlayerBaseInfo& stPlayerBase,
            CPlayerDetailInfo& stPlayerDetail, MsgTips& stTips);

    void NotifyActListToRole(CPlayerBaseInfo& stPlayerBase,
            CPlayerDetailInfo& stPlayerDetail);

    int GetActTimerSize() const {return _astActTimer.Size();}
    CActTimer& GetActTimer(int iIndex){return _astActTimer[iIndex];}
    void AddActTimer(CActTimer& stActTimer)
    {
        _astActTimer.AddOneItem(stActTimer);
    }
    void DealActTimer();

    int GetActNextOpenTime (short shActID, int iNowTime, int & iNextOpenTime);

    //下发成长基金状态
    void SendGrowFundInfo(CPayActData& stPayActData, unsigned int uiGID);

    //宝箱争夺战: 获取信息
    void GetFightForBoxInfo(CFightForBoxData& stUserData, MsgAnsTreasureBox& stAns);
    void SendFightForBoxInfoToFront(unsigned int uiGID, CFightForBoxData& stUserData);

    //宝箱争夺战: 开始
    int StartFightForBox(CFightForBoxData& stUserData);

    //宝箱争夺战: 获取所有有效的宝箱下标
    void GetValidBoxIndex(CFightForBoxData& stUserData, CTArray<int, MAX_FIGHT_FOR_BOX_BOX_COUNT>& aiIndex);

    //宝箱争夺战: 打开宝箱
    int OpenFightForBox(CFightForBoxData& stUserData, unsigned int uiGID, int iBoxPos, MsgAnsTreasureBox& stAns);

    //宝箱争夺战: 积分兑换
    int GetFightForBoxBonus(CFightForBoxData& stUserData, unsigned int uiGID, int iBonusID, bool bChangeAll, MsgAnsTreasureBox& stAns);
        
    //宝箱争夺战: 使用慧眼
    int LookFightForBox(CFightForBoxData& stUserData, int iBoxPos, MsgAnsTreasureBox& stAns);

    //宝箱争夺战: 购买慧眼
    int FightForBoxBuyEye(CFightForBoxData& stUserData, CPlayerBaseInfo& stPlayerBaseInfo, MsgAnsTreasureBox& stAns);    

    void GetNewZoneSalesInfo(CNewZoneSales & stUserData, MsgAnsNewZoneSales & stAns);

    int BuyNewSalesItem (CPlayerBaseInfo & stPlayerBase, CNewZoneSales & stUserData, int iPos, CDropItem & stDropItem);

    int BuyNewSalesItemAll (CPlayerBaseInfo & stPlayerBase, CNewZoneSales & stUserData, CDropItem & stDropItem);

    int GetNewSalesBonus (CPlayerBaseInfo & stPlayerBase, CNewZoneSales & stUserData, CDropItem & stDropItem);

    bool IsNewSalesOpen (CPlayerBaseInfo & stPlayerBase);
private:
    //更新精彩活动的数据
    void UpdateSplendidActData(unsigned int uiGID);
    void UpdateSplendidActData(unsigned int uiGID, int iActID);

    //计算精彩活动的时间
    void GetSplendidActTime(const char* szTime, int& iTime, int iBaseTime);

public:
    CMineCrystalConfig _stMineConfig;
    CEnemyAtkCityEnemyConfig _stEnemyAtkCityConfig; //怪物攻城战数据
    CEnemyAtkCityBonusConfig _stEnemyAtkCityBonusConfig; //怪物攻城战奖励数据
    CClanBattleBonusConfig  _stClanBattleBonusConfig;       //家族战奖励配置
    CPyramidConfig _stPyramidConfig;            //金字塔活动
    CNewbieTaskConfig _stNewbieTaskConfig;      //新手目标活动

private:
    int     _iLastTime;         //上次遍历时间保证每秒只遍历一遍
    int     _iSysStopTime;      //系统停机维护时刻
    CTSizeString<ROLENAME_LEN>  _szSysStopTime;//系统停机时间串
    CTSizeString<ROLENAME_LEN>  _szSysStartTime;//系统开机时间串
    int     _iNextNoticeSysStop;//上次公告系统停机维护时刻
    CActCfg _stActCfg;
    CTLib::CTArray<short, MAX_ACT_CONFIG_COUNT> _ashNotifyAct;
    CTLib::CTArray<int, MAX_ACT_CONFIG_COUNT> _aiNotifyTip;
    CTLib::CTArray<int, MAX_ACT_CONFIG_COUNT> _aiUpdateTime;
    CTLib::CTArray<int, MAX_SYSTEM_NOTICE_CONFIG_COUNT> _aiSysNotice;
    CTLib::CTArray<CActTimer, MAX_ACT_TIMER_COUNT> _astActTimer;
    CActStatusCfg _stActStatusCfg;  //活动状态配置
    CSplendidActConfig _stSplendidActConfig;    //精彩活动配置
    CSplendidActConfigDetail _stSplendidActConfigDetail; //精彩活动详细配置
    CSystemNoticeCfg _stSystemNoticeCfg;    //系统提示配置
    CFightForBoxConfig _stFightForBoxConfig;  //宝箱争夺战配置
    CFightEndConfig _stFightEndConfig;  //一战到底配置
    CNewZoneSalesConfig _stNewZoneSalesConfig;  //开服特卖配置
};

#endif //__CActMgr_h__
